package com.singbon.controller;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.env.Environment;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author xxx
 */
@Slf4j
@Controller
public class CommonController {
  private final Environment env;
  private final ContextRefresher contextRefresher;
  @Autowired
  public CommonController(ContextRefresher contextRefresher, Environment env) {
    this.contextRefresher = contextRefresher;
    this.env = env;
  }
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  }
  /**
   * logout.do 可以退出
   * logout 不能退出
   * @param request
   * @param response
   * @throws IOException
   */
  @RequestMapping(value = "/logout.do", method = {RequestMethod.POST, RequestMethod.GET})
  public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
    contextRefresher.refresh();
    String[] activeProfiles = env.getActiveProfiles();
    log.info("activeProfiles:" + JSON.toJSONString(activeProfiles));
    String ssoServer = env.getProperty("sso-server");
    String logoutUrl = ssoServer + "/zk0vxsxuq7/logout";
    log.info("logoutUrl = " + logoutUrl);
    response.sendRedirect(logoutUrl);
  }
  /**
   * 主页
   */
  @RequestMapping(value = "/main")
  public ModelAndView main(HttpServletRequest request, HttpServletResponse response, Model model) {
    contextRefresher.refresh();
    String[] activeProfiles = env.getActiveProfiles();
    log.info("activeProfiles:" + JSON.toJSONString(activeProfiles));
    String loginSsoDevice = env.getProperty("sso-device");
    model.addAttribute("loginSsoDevice", loginSsoDevice);
    String loginSsoBalance = env.getProperty("sso-balance");
    model.addAttribute("loginSsoBalance", loginSsoBalance);
    String loginSsoHptFacePos = env.getProperty("sso-htp-face-pos");
    model.addAttribute("loginSsoHptFacePos", loginSsoHptFacePos);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String name = authentication.getName();
    log.info("name = " + name);
    return new ModelAndView("/main");
  }
}
